package tutorias.conexion;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import sistemas.sam.R;
//import tutorias.SeleccionarAsesoriaActivity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.view.View;

public class Conexionbd {
	JSONArray jArray=null;
	String result = null;
	String matricula,materia_nom;
	InputStream is = null;
	StringBuilder sb = null;
	String grupo2="";
	String dirr="http://192.168.61.143/SAM/";
	String []materias;
	String []profesores;
	String []prueba;
	String php,variable,valor,campo;
	private ConectarSam mAuthTask = null;

	public String[] Materia(String matrin,String variablen,String phpn,String campo_obtener){
		php=phpn;
		variable=variablen;
		valor=matrin;
		campo=campo_obtener;
		mAuthTask = new ConectarSam();
		mAuthTask.execute((Void) null);
		materias=prueba;
		return materias;
	}
	
	public String[] Profesores(String materia,String var,String phpn){	
		int materia_id=0;
		php="sacarid";
		variable="nombre";
		valor=materia;
		mAuthTask = new ConectarSam();
		mAuthTask.execute((Void) null);	
		try {
			jArray = new JSONArray(result);
			JSONObject json_data = null;
			for (int i = 0; i < jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				materia_id= json_data.getInt("materia_id");		
				Log.d("msg", "paso  "+materia_id);
			}
		} catch (JSONException e1) {
			Log.e("JSON ERR", e1.getMessage());			
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		String mate_id=""+materia_id;
		php=phpn;
		variable=var;
		valor=mate_id;
		mAuthTask = new ConectarSam();
		mAuthTask.execute((Void) null);	
		
		return profesores;
	}

public class ConectarSam extends AsyncTask<Void, Void, Boolean> {
	@Override
	protected Boolean doInBackground(Void... params) {
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		// http post
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(dirr+php+
					".php?"+variable+"="+valor		);
			Log.v("log_tag", dirr+php+
					".php?"+variable+"="+valor);
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", " "
					+ e.toString());
		}
		try {
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is, "iso-8859-1"), 8);
			sb = new StringBuilder();
			sb.append(reader.readLine() + "\n");

			String line = "0";
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result = sb.toString();
			Log.v(" obtenido", result);
		} catch (Exception e) {
			Log.e("log_tag",
					"Error convirtiendo datos recibidos" + e.toString());
		}
		try {
			jArray = new JSONArray(result);
			JSONObject json_data = null;
			prueba =new String[jArray.length()]; 
			for (int i = 0; i < jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				prueba[i]= json_data.getString(campo);		
				Log.d("msg", "paso  "+prueba[i]);
			}
		} catch (JSONException e1) {
			Log.e("JSON ERR", e1.getMessage());
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		return true;
	}
	@Override
	protected void onPostExecute(final Boolean success) {
		mAuthTask = null;
		//showProgress(false);
		Log.v("login_resultado", success.toString());
		if (success) {
			
		

		} else {
			
		}
	}

	@Override
	protected void onCancelled() {
		mAuthTask = null;
		//showProgress(false);
	}
}


}
